#include <HomeEasy.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085.h>
#include "DHT.h"
#define DHTPIN 12     // what pin we're connected to

#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);
/********* Global  Variables  ***********/
Adafruit_BMP085 bmp = Adafruit_BMP085(10085);
HomeEasy homeEasy;
int myCode = 0000000;
int ZoneStates[4];
int heatingstate;
String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
unsigned long checkzonestimer;
void setup(){
  homeEasy = HomeEasy();
  homeEasy.init();
  if (!bmp.begin()) {
    Serial.println("Could not find a valid BMP085 sensor, check wiring!");
  }
  dht.begin();
  sendRF(myCode,1,true,false);
  delay(3000);
  sendRF(myCode,1,false,false);
  pinMode(3,OUTPUT);
  pinMode(4,INPUT_PULLUP);
  pinMode(5,INPUT_PULLUP);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(9,OUTPUT);
  Serial.begin(9600);
  digitalWrite(6,HIGH);          
  digitalWrite(7,HIGH);        
  digitalWrite(10,HIGH);          
  digitalWrite(3,HIGH);
  inputString.reserve(200);
  checkzonestimer = millis();
}

String txtMsg2 = "";                         // a string for incoming text
int lastStringLength2 = txtMsg2.length();     // previous length of the String


void loop(){

  if(millis() > (checkzonestimer + 100)) { // 5 mins
    //    Serial.println("Checking Zones");
    checkzones();
    checkzonestimer = millis();
  }
  if (stringComplete) {
    ProcessData(inputString); 
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}
void sendRF(unsigned long lSender,unsigned int iRecipient, boolean bOn,boolean bGroup){
  homeEasy.sendAdvancedProtocolMessage(lSender,iRecipient,bOn,bGroup);
  homeEasy.sendAdvancedProtocolMessage(lSender,iRecipient,bOn,bGroup);
  homeEasy.sendAdvancedProtocolMessage(lSender,iRecipient,bOn,bGroup);
}


int heatinglaststate = 0;

void ProcessData(String Data){

  Data.replace("@","");
  Data.replace("\\","");

  int i, count; 
  for (i=0, count=0; Data[i]; i++) 
    count += (Data[i] == ','); 

  String newData[count];

  for(int i =0; i < count;i++){
    int firstpos = Data.indexOf(",");
    newData[i] = Data.substring(0,firstpos);
    Data = Data.substring(firstpos+1,Data.length());
  }
  //  Serial.println(newData[0]);
  if(newData[0] == "alarm"){
    if(convint(newData[1]) == 1){
      digitalWrite(6,LOW);    
    }
    else    {
      digitalWrite(6,HIGH);        
    }
  }
    if(newData[0] == "led"){
        if(convint(newData[1]) == 1){
      digitalWrite(3,LOW);    
    }
    else    {
      digitalWrite(3,HIGH);        
    }
  }
  if(newData[0] == "heating"){
    if(convint(newData[1]) == 1){
      digitalWrite(7,LOW);    
    }
    else    {
      digitalWrite(7,HIGH);        
    }
  }
  if(newData[0] == "hotwater"){
    if(convint(newData[1]) == 1){
      digitalWrite(10,LOW);    
    }
    else    {
      digitalWrite(10,HIGH);        
    }
  }  
  if(newData[0] == "lights"){
    boolean lstate=false;
    boolean gstate=false;


    if (convint(newData[3]) == 1){
      lstate=true;
    } 
    else {
      lstate=false;
    }

    if (convint(newData[4]) == 1){
      gstate=true;
    } 
    else {
      gstate=false;
    }

    sendRF(convint(newData[1]),convint(newData[2]),lstate,gstate); 

  }
}
int convint(String Data){
  char carray2[Data.length() +1];
  Data.toCharArray(carray2,sizeof(carray2));
  return atoi(carray2); 
}
boolean zone1last = false;
boolean zone2last = false;
boolean zone3last = false;
boolean zone4last = false;

void checkzones(){
  sensors_event_t event;
  bmp.getEvent(&event);

  if(Serial){
    //    Serial.print("Zone,1,");
    Serial.print(ZoneState(analogRead(A0)));
    Serial.print(",");
    Serial.print(ZoneState(analogRead(A1)));
    Serial.print(",");
    Serial.print(ZoneState(analogRead(A2)));
    Serial.print(",");
    Serial.print(ZoneState(analogRead(A3)));
    Serial.print(",");
    Serial.print(digitalRead(4));
    Serial.print(",");    
    Serial.print(digitalRead(5));
    Serial.print(",");        
    if (event.pressure)
    {
      Serial.print(event.pressure);
    }
    else{
      Serial.print("0");
    }
    Serial.print(",");
    float h = dht.readHumidity();
    Serial.print(h);
    Serial.println(",");            
    //Serial.println(analogRead(A1));

  }
}

int ZoneState(int val){
  if(val < 740){
    return 1;
  }
  else if (val > 809){
    return 0;
  }
  else {
    return 0; 
  }

}


void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read(); 
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    } 
  }
}



